package com.chzc.fm.duotin.adapter.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.bean.channel.AnchorSpecial;
import com.chzc.fm.duotin.bean.channel.AnchorSpecialItem;
import com.chzc.fm.duotin.utils.ImageUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/6/25.
 */
public class HotAnLinearLayoutAdapter extends RecyclerView.Adapter<HotAnLinearLayoutAdapter.HotAnLinelayoutViewholder>{

    private LayoutInflater inflater;

    private List<AnchorSpecial> specialList;

    private mOnItemClickListener itemClickListener;

    private Context context;

    public HotAnLinearLayoutAdapter(Context context, List<AnchorSpecial> specialList) {
        this.context = context;
        this.specialList = specialList;

        inflater = LayoutInflater.from(context);
    }

    /**
     * 设置监听
     * @param itemClickListener
     */
    public void setOnItemClickListener(mOnItemClickListener itemClickListener )
    {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public HotAnLinelayoutViewholder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView =  inflater.inflate(R.layout.hot_anchor_pannel,null);

        return new HotAnLinelayoutViewholder(itemView);
    }

    @Override
    public void onBindViewHolder(HotAnLinelayoutViewholder holder, final int position) {

        AnchorSpecial special = specialList.get(position);

        List<AnchorSpecialItem> itemlist = special.getTags();

        String line = "";

        ImageLoader.getInstance().displayImage(special.getImage_url(), holder.ivmain, ImageUtil.getDefaultOptions());

        holder.tvplaynum.setText(special.getPlay_num() + "");

        holder.tvtitle.setText(special.getTitle());

        for (int i = 0; i < itemlist.size(); i++) {
            AnchorSpecialItem item = itemlist.get(i);

            line = item.getName() + " " + line;
        }

        holder.tvtag.setText(line);

        holder.tvdescribe.setText(special.getDescribe());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                itemClickListener.OnItemClick(position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return specialList == null ? 0 : specialList.size() ;
    }

    public class HotAnLinelayoutViewholder extends RecyclerView.ViewHolder
    {
        TextView tvplaynum,tvtitle,tvtag,tvdescribe,tvmark;
        ImageView ivmain;

        public HotAnLinelayoutViewholder(View itemView) {
            super(itemView);

            tvplaynum = (TextView) itemView.findViewById(R.id.hot_anchor_play_tv);
            tvtitle = (TextView) itemView.findViewById(R.id.hot_anchor_title_tv);
            tvtag = (TextView) itemView.findViewById(R.id.hot_anchor_tag_tv);
            tvdescribe = (TextView) itemView.findViewById(R.id.hot_anchor_describe_tv);
            ivmain = (ImageView) itemView.findViewById(R.id.hot_anchor_main_iv);
        }
    }

    public interface mOnItemClickListener
    {
        void OnItemClick(int position);
    }
}
